# -*- coding: utf-8 -*-
"""
工具函数模块
"""

import os
import datetime
from pathlib import Path

def ensure_dir(directory):
    """确保目录存在"""
    Path(directory).mkdir(parents=True, exist_ok=True)
    return directory

def get_timestamp():
    """获取当前时间戳字符串"""
    return datetime.datetime.now().strftime("%Y%m%d_%H%M%S")

def get_date_string():
    """获取当前日期字符串"""
    return datetime.datetime.now().strftime("%Y-%m-%d")

def format_file_size(size_bytes):
    """格式化文件大小显示"""
    if size_bytes == 0:
        return "0B"
    size_names = ["B", "KB", "MB", "GB", "TB"]
    i = 0
    while size_bytes >= 1024 and i < len(size_names) - 1:
        size_bytes /= 1024.0
        i += 1
    return f"{size_bytes:.1f}{size_names[i]}"